#include "Common.h"

void CommonSceneParameters::Init(const string &parametersFilename)
{
	cout << "Loading parameters.txt....." << endl;

	bool loadedDirectory = false;
	bool loadedOutputDir = false;
	// TODO : take this out later
	bool loadedDefaultModel = false;

    vector<string> fileLines = Utility::GetFileLines(parametersFilename, 1);

	for (unsigned int lineNumber = 0; lineNumber < fileLines.size(); lineNumber++) {
	    const string &curLine = fileLines.at(lineNumber);

		// We ignore comments (lines starting with #)
		if (curLine.at(0) == '#') {
			continue;
		}

		// Get the database directory (we need to add the extra "/" at the
		// end if it isn't there already, here we use push_back)
		if (curLine.find("databaseDirectory") == 0)
		{
			databaseDirectory = Utility::PartitionString(curLine, "=")[1];
			if(databaseDirectory[databaseDirectory.length() - 1] != '/')
			{
				databaseDirectory.push_back('/');
			}
			cout << "Models and textures will be read from: " << databaseDirectory << endl;
			loadedDirectory = true;
		}

		// Loads the default model for ModelViewer
		else if(curLine.find("textQuery") == 0)
		{
			textQuery = Utility::PartitionString(curLine, "=")[1];
			cout << "Default Text Query will be: " << textQuery << endl;
			loadedDefaultModel = true;
		}

		// Loads the default model for ModelViewer
		else if(curLine.find("outputDirectory") == 0)
		{
			outputDirectory = Utility::PartitionString(curLine, "=")[1];
			if(outputDirectory[outputDirectory.length() - 1] != '/')
			{
				outputDirectory.push_back('/');
			}
			cout << "Screenshots will be written to: " << outputDirectory << endl;
			loadedOutputDir = true;
		}
	}

	if (!loadedDirectory || !loadedOutputDir || !loadedDefaultModel) {
		cout << "Error: invalid parameters.txt. You must list the input/output directories, and the default model to load." << endl;
		cout << "       Make sure the file contains these lines specifying:" << endl;
		cout << "		databaseDirectory=<scene database directory>" << endl;
		cout << "		outputDirectory=<dir to place the screenshots>" << endl;
		cout << "		textQuery=<default model for ModelViewer>" << endl;
		cout << "[Press any key to exit]" << endl;
		getchar();
		exit(3);
	}

	cout << "Finished loading parameters!" << endl;
}
